using System;
using System.Collections.Generic;
using System.Collections;
using System.Xml;
using System.Text;

namespace GURUCORE.Portal.Core.RSS
{
    public class RSS20
    {
        XmlTextWriter writer = null;

        public string title;
        public string link;
        public string description;
        public string docs;
        public string webMaster;
        public string pubDate;
        public string language;
        public string copyright;
        public string managingEditor;
        public string lastBuildDate;
        public string category;
        public string generator;
        public string cloud;
        public string ttl;
        public string rating;
        public string skipHours;
        public string skipDays;
        public string image_url;
        public string image_link;
        public string image_title;
        public string image_width;
        public string image_height;
        public string image_description;

        public string textInput_title;
        public string textInput_description;
        public string textInput_name;
        public string textInput_link;

        public ArrayList Items = new ArrayList();

        string sWhereToSaveFile;
        Encoding encoding = null;
        public RSS20(string p_sWhereToSaveFile, Encoding p_encoding)
        {
            sWhereToSaveFile = p_sWhereToSaveFile;
            encoding = p_encoding;
        }

        public void Save()
        {
            try
            {
                writer = new XmlTextWriter(sWhereToSaveFile, encoding);

                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                writer.Namespaces = false;
                writer.WriteStartDocument();

                writer.WriteStartElement("", "rss", "");
                writer.WriteAttributeString("version", "2.0");

                #region channel

                writer.WriteStartElement("", "channel", "");

                #region channel info

                writer.WriteStartElement("", "title", ""); writer.WriteString(title); writer.WriteEndElement();
                writer.WriteStartElement("", "link", ""); writer.WriteString(link); writer.WriteEndElement();
                writer.WriteStartElement("", "description", ""); writer.WriteString(description); writer.WriteEndElement();

                writer.WriteStartElement("", "docs", ""); writer.WriteString(docs); writer.WriteEndElement();
                writer.WriteStartElement("", "webMaster", ""); writer.WriteString(webMaster); writer.WriteEndElement();
                writer.WriteStartElement("", "pubDate", ""); writer.WriteString(pubDate); writer.WriteEndElement();
                writer.WriteStartElement("", "language", ""); writer.WriteString(language); writer.WriteEndElement();
                writer.WriteStartElement("", "copyright", ""); writer.WriteString(copyright); writer.WriteEndElement();
                writer.WriteStartElement("", "managingEditor", ""); writer.WriteString(managingEditor); writer.WriteEndElement();
                writer.WriteStartElement("", "lastBuildDate", ""); writer.WriteString(lastBuildDate); writer.WriteEndElement();
                writer.WriteStartElement("", "category", ""); writer.WriteString(category); writer.WriteEndElement();
                writer.WriteStartElement("", "generator", ""); writer.WriteString(generator); writer.WriteEndElement();
                // writer.WriteStartElement("", "cloud", ""); writer.WriteString(cloud); writer.WriteEndElement();
                writer.WriteStartElement("", "ttl", ""); writer.WriteString(ttl); writer.WriteEndElement();
                writer.WriteStartElement("", "rating", ""); writer.WriteString(rating); writer.WriteEndElement();
                writer.WriteStartElement("", "skipHours", ""); writer.WriteString(skipHours); writer.WriteEndElement();
                writer.WriteStartElement("", "skipDays", ""); writer.WriteString(skipDays); writer.WriteEndElement();


                writer.WriteStartElement("", "image", "");
                writer.WriteStartElement("", "url", ""); writer.WriteString(image_url); writer.WriteEndElement();
                writer.WriteStartElement("", "link", ""); writer.WriteString(image_link); writer.WriteEndElement();
                writer.WriteStartElement("", "title", ""); writer.WriteString(image_title); writer.WriteEndElement();
                writer.WriteStartElement("", "width", ""); writer.WriteString(image_width); writer.WriteEndElement();
                writer.WriteStartElement("", "height", ""); writer.WriteString(image_height); writer.WriteEndElement();
                writer.WriteStartElement("", "description", ""); writer.WriteString(image_description); writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("", "textInput", "");
                writer.WriteStartElement("", "title", ""); writer.WriteString(textInput_title); writer.WriteEndElement();
                writer.WriteStartElement("", "description", ""); writer.WriteString(textInput_description); writer.WriteEndElement();
                writer.WriteStartElement("", "name", ""); writer.WriteString(textInput_name); writer.WriteEndElement();
                writer.WriteStartElement("", "link", ""); writer.WriteString(textInput_link); writer.WriteEndElement();
                writer.WriteEndElement();

                #endregion channel info

                #region Item repeat

                foreach (RSS_Item item in Items)
                {
                    item.WriteTo(writer);
                }

                #endregion

                writer.WriteEndElement();
                #endregion channel

                writer.WriteEndElement();   // rss

                writer.WriteEndDocument();
                writer.Flush();
            }
            catch (Exception ex)
            {
                ex.ToString();
                // MessageBox.Show("Exception: {0}", e.ToString());
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }

    }
}
